<?php

/**
 * AuthController
 * 
 * @author Maodo DIOP <dmaodo@gmail.com>
 * @version 
 */

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class Admin_PointterrainController extends Zend_Controller_Action {
	
	public function indexAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$pointterrain = new Admin_Model_DbTable_Pointterrain();
		$res = $pointterrain->getAllPointterrain();
		$resultat = Zend_Json :: encode($res);
		//$this->_response->setHeader('Content-Type', 'text/x-json');
		$this->_response->setBody($resultat);
	}
	
	public function addAction() {
		$this->view->title = "Ajouter Point GMV";
		$this->view->headTitle($this->view->title);
		
		if ($this->getRequest()->isPost()) {
			$latitude = stripslashes($this->_request->getPost('latitude'));
			$longitude = stripslashes($this->_request->getPost('longitude'));
			$idmatricebr = stripslashes($this->_request->getPost('idmatricebr'));
			$pointterrainprec = stripslashes($this->_request->getPost('pointterrainprec'));
			$pointterrain = new Admin_Model_DbTable_Pointterrain();
			$pointterrain->addPointterrain($latitude,$longitude,$idmatricebr, $pointterrainprec);                
			$this->_helper->redirector('index');
		}
	}
	
	public function addIJAction() {
        $this->view->title = "Ajouter nouvel ";
        $this->view->headTitle($this->view->title);

        $i = $this->_getParam('i', 0);
        $j = $this->_getParam('j', 0);
        $pointterrain = new Admin_Model_DbTable_Pointterrain();
        $pointterrain->addArbreIJ($i,$j); 
    }

	public function editAction() {
		$form = new Admin_Form_Pointterrain() ;
		
		$form->submit->setLabel('Enregistrer');
		$this->view->form = $form;
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {            	
				
				$idpointterrain = (int)$this->_getParam('idpointterrain');;             
				$latitude = $form->getValue('latitude');
				$longitude = $form->getValue('longitude');
				$pointterrainprec = $form->getValue('pointterrainprec');
				$pointterrain = new Admin_Model_DbTable_Pointterrain();
				
				$pointterrain = new Admin_Model_DbTable_Pointterrain();
				$pointterrain->updatePointterrain($idpointterrain, $latitude, $longitude, $pointterrainprec); 
				$res = array(); 
				$res = $pointterrain->getPointterrain($idpointterrain);        
				$this->_redirect('/admin/pointterrain/index');
			} else {
				$form->populate($formData);
			}
		} else {
			//Si le formulaire n'est pas encore post�
			//Recuperer la valeur de l'objet � editer
			$idpointterrain = $this->_request->getParam('idpointterrain', 0);
			if ($idpointterrain > 0) {
				$pointterrain = new Admin_Model_DbTable_Pointterrain();
				$form->populate($pointterrain->getPointterrain($idpointterrain));
			}
		} 
	}
	
	
	public function deleteAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		
		$idpointterrain = $this->_getParam('idpointterrain', 0);
		$pointterrain = new Admin_Model_DbTable_Pointterrain();
		$pointterrain->deletePointterrain($idpointterrain);
		$this->_redirect('/pointterrain/index');
	}
	
	public function allpointterrainAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$pointterrain = new Admin_Model_DbTable_Pointterrain();
		$res = $pointterrain->getAllPointterrain();
		$resultat = Zend_Json :: encode($res);
		//$this->_response->setHeader('Content-Type', 'text/x-json');
		$this->_response->setBody($resultat);
	}
}

